package org.mongodb;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageReaderUtil {
      /**
       * @param
       *     imageUrl
       * @return
       *    byte array of the file
       */
      public  static byte[] getBytesByURL(String imageUrl) {
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           BufferedInputStream bis = null;
           HttpURLConnection urlconnection= null;
           URL url = null;
           byte[] buf = new byte[1024];
           try {
               url = new URL(imageUrl);
               urlconnection = (HttpURLConnection) url.openConnection();
               urlconnection.connect();
               bis = new BufferedInputStream(urlconnection.getInputStream());
               for (int len = 0; (len = bis.read(buf)) != -1;){
                    baos.write(buf,0,len);
               }
               return baos.toByteArray();
           } catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
               try {
                    urlconnection.disconnect();
                    bis.close();
               } catch (IOException ignore) {
               }
           }
           return baos.toByteArray();
      }
      
      public static void main(String[] args) {
    	  String imageUrl = "http://web.qq.com/cgi-bin/get_group_pic?type=0&gid1303895317&uin=3962040174&rip=123.138.238.110&rport=8000&fid=2938177918&pic={3443108D-13C7-E7BF-9182-CDD170E18410}.jpg&vfwebqq=86e1207d0a965a631676f708e47e051702516104ed2cfee4649bcd6e9cdf223eb26d693bf072e8cdt=1358524027/";
    	  byte[] bytesByURL = ImageReaderUtil.getBytesByURL(imageUrl );
    	  for (int i = 0; i < bytesByURL.length; i++) {
			System.out.println(bytesByURL[i]);
		}	
	}
 }